<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%hj_project_child}}".
 *
 * @property int $id 序号
 * @property string $name 项目名称
 * @property string $comment 备注
 * @property int $origin_type 项目来源：1-合同签订 2-委托书 3-中标通知书 4-口头订单
 * @property int $branch_id 承建分公司
 * @property int $leading_user_id 生产负责人
 * @property int $progress_status 0-进行中 1-已完成
 * @property string $content 项目内容
 * @property int $customer_id 甲方客户id
 * @property int $plan_started_at 项目生产开始时间
 * @property int $plan_completed_at 项目生产计划完成时间
 * @property string $money_total 合同金额
 * @property string $money_received 已收款金额
 * @property string $money_pay_way 分几期如何付
 * @property int $fee_percentage 百分之几的管理费
 * @property int $contract_type 合同类型1-自主2-挂靠3-分包
 * @property int $input_user_id 录入员
 * @property int $created_at 记录创建时间
 * @property int $updated_at 记录修改时间
 * @property int $valid 1-有效 0-无效
 * @property int $sign_status 0未签订,1已签订
 * @property int $sign_at 项目签订时间
 * @property int $complete_at 项目完成时间
 * @property string $checker 校准人
 * @property string $proofreader 审核人
 * @property int $flow_status 分配状态,0未分配,1已分配
 * @property string $planner 设计人
 * @property int $research_cost 可研费
 * @property int $early_cost 前期费
 * @property int $design_cost 设计费
 
 * @property int $make_cost 造价编制费
 */
class ProjectChild extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%project_child}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['origin_type', 'branch_id', 'leading_user_id', 'plan_started_at', 'plan_completed_at', 'progress_status', 'input_user_id', 'created_at', 'updated_at', 'valid', 'customer_id', 'fee_percentage',
                'contract_type','sign_status','sign_at','complete_at','flow_status', 'research_cost', 'early_cost', 'design_cost', 'make_cost', 'design_stage', 'figure', 'project_type_id', 'department_id',
                'project_total','project_cost', 'project_complete', 'prospect_cost', 'project_id', 'receive_status'], 'integer'],
            [['content', 'checker', 'proofreader', 'planner', 'project_no', 'budgeter', 'team_worker', 'pay_way'], 'string'],
            [['money_total', 'money_received'], 'number'],
            [['name'], 'string', 'max' => 255],
            [['name'], 'unique'],
        ];
    }

    public function extraFields()
    {
        $fields['leading_user_name'] = function ($model){
            return $model->leadingUser->username;
        };
        $fields['customer_name'] = function ($model){
            return $model->customer->name;
        };

        $fields['childFlow'] = function ($model){
            return $model->childFlow;
        };
        return $fields;
    }

    public function getLeadingUser(){
        return $this->hasOne(User::class,['id'=>'leading_user_id']);
    }

    public function getCustomer(){
        return $this->hasOne(Customer::class,['id'=>'customer_id']);
    }

    public function getChildFlow(){
        return $this->hasOne(ChildFlow::class,['project_child_id'=>'id']);
    }
}
